/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.unix;

import java.util.HashSet;
import java.util.Set;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Context;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.FalseExit;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides implementation for signal-related primitives.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Signals {

    /**
     * No instance of this class.
     */
    private Signals() {
    } // end empty constructor

    /**
     * Modifies signal mask.
     * @param ctxt context
     * @param vaction how passed signal set will be interpreted:
     *                <ul>
     *                  <li><tt>0:</tt> new mask is equal to set;</li>
     *                  <li><tt>1:</tt> new mask is current set plus passed set;</li>
     *                  <li><tt>2:</tt> new mask is current set minus passed set.</li>
     *                </ul>
     * @param vset signal set
     * @return old signal mask
     */
    @Primitive
    public static Value unix_sigprocmask(final CodeRunner ctxt,
                                         final Value vaction,
                                         final Value vset) {
        final Set<Integer> mask =
            ctxt.getContext().blockSignals(vaction.asLong(),
                                           fr.x9c.cadmium.kernel.Signals.decodeSignalSet(vset));
        return fr.x9c.cadmium.kernel.Signals.encodeSignalSet(mask);
    } // end method 'unix_sigprocmask(CodeRunner, Value, Value)'

    /**
     * Returns the set of currently blocked signals.
     * @param ctxt context
     * @param unit ignored
     * @return the set of currently blocked signals
     */
    @Primitive
    public static Value unix_sigpending(final CodeRunner ctxt,
                                        final Value unit) {
        return fr.x9c.cadmium.kernel.Signals.encodeSignalSet(ctxt.getContext().getPendingSignals());
    } // end method 'unix_sigpending(CodeRunner, Value)'

    /**
     * Suspends execution until a signal is caught.
     * @param ctxt context
     * @param vset set of signal to wait for
     * @return <i>unit</i>
     * @throws FalseExit if another thread exits the program
     * @throws Fail.Exception if an asynchronous exception has been thrown
     */
    @Primitive
    public static Value unix_sigsuspend(final CodeRunner ctxt,
                                        final Value vset)
        throws FalseExit, Fail.Exception {
        final Context context = ctxt.getContext();
        context.enterBlockingSection();
        fr.x9c.cadmium.kernel.Signals.waitForSignal(ctxt.getContext(),
                                                    fr.x9c.cadmium.kernel.Signals.decodeSignalSet(vset));
        context.leaveBlockingSection();
        return Value.UNIT;
    } // end method 'unix_sigsuspend(CodeRunner, Value)'

} // end class 'Signals'
